package com.example.core

import android.annotation.SuppressLint
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

abstract class BaseViewHolder : RecyclerView.ViewHolder {

    constructor(itemView: View) : super(itemView)

    @SuppressLint("UseSparseArrays")//有待明确，为什么要用这个
    private val viewHashMap = HashMap<Int, View>()

    protected fun <T : View> getView(id: Int): T {
        var view = viewHashMap.get(id)
        if (view == null) {
            view = itemView.findViewById(id)
            viewHashMap.put(id, view)
        }
        return view as T
    }

    protected fun setText(id: Int, text: String?) {
        getView<TextView>(id).text = text
    }
}